package com.acce.gxsc.page.outCarHandover;

import android.graphics.Bitmap;
import android.view.View;

import com.acce.gxsc.common.App;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.bean.ReportFormEvent;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.NetOutCarBean;
import com.acce.gxsc.model.netBean.NetOutCarReport;
import com.acce.gxsc.model.netBean.ReportFormBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.base.volleyHttp.GsonUtils;
import com.acce.sdk.utils.LogUtils;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.sunmi.impl.V1Printer;
import com.sunmi.util.ThreadPoolManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Set;

import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by huxley on 2017/5/26.
 */

public class OutCarHandoverPresenter extends BasePresenter<OutCarHandoverView> {
    private V1Printer printer;
    private Bitmap    orderBitmap;

    void printOrder(final View orderView) {
        if (mViewRef != null) {
            getView().showProgressDialog("打印中...", false);
            if (orderBitmap == null) {
                orderBitmap = Tools.convertViewToBitmap(orderView, 0.75f);
            }
            orderView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        ThreadPoolManager.getInstance().executeTask(new Runnable() {
                            @Override
                            public void run() {
                                if (printer == null) {
                                    printer = new V1Printer(App.context);
                                }
                                printer.beginTransaction();
                                printer.setAlignment(1);
                                printer.printBitmap(orderBitmap);
                                printer.lineWrap(4);
                                printer.printHorizontalLine(1, 0);
                                printer.lineWrap(2);
                                printer.commitTransaction();
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                if (mViewRef != null) {
                                    orderView.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            getView().hideProgressDialog();
                                            getView().printSuccess();
                                        }
                                    }, 1500);
                                }
                            }
                        });
                    } catch (Exception e) {
                        ToastUtils.show("打印失败！");
                    }
                }
            }, 300);
        }
    }

    public void loadData() {
        WorkModel.getInstance().loadOutCarForm().map(new Func1<ResultBean<NetOutCarReport>, ArrayList<ArrayList<ReportFormBean>>>() {
            @Override
            public ArrayList<ArrayList<ReportFormBean>> call(ResultBean<NetOutCarReport> netInCarReportResultBean) {
                if (netInCarReportResultBean == null || netInCarReportResultBean.result == null) {
                    return new ArrayList<>();
                }
                return getData(netInCarReportResultBean.result.outCarReport);
            }
        }).subscribe(new Subscriber<ArrayList<ArrayList<ReportFormBean>>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("提交中...", false);
                }
            }

            @Override
            public void onNext(ArrayList<ArrayList<ReportFormBean>> datas) {
                if (mViewRef != null) {
                    LogUtils.logout(GsonUtils.toJson(datas));
                    getView().loadSuccess(datas);
                }
            }

            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }


    public ArrayList<ArrayList<ReportFormBean>> getData(ArrayList<NetOutCarBean> inCarReport) {
        int num = 0;
        if (inCarReport != null) {
            num = inCarReport.size();
        }
        EventBus.getDefault().post(new ReportFormEvent(num));
        ArrayList<ArrayList<ReportFormBean>> data = new ArrayList<>();
        data.add(new ArrayList<ReportFormBean>());
        data.add(new ArrayList<ReportFormBean>());
        data.add(new ArrayList<ReportFormBean>());
        if (inCarReport == null) {
            return data;
        }

        for (int i = 0; i < inCarReport.size(); i++) {
            int i1 = -1;
            for (int j = 0; j < data.get(0).size(); j++) {
                if (data.get(0).get(j).title.equals(inCarReport.get(i).goodsTypeInfo) || (data.get(0).get(j).title.equals("未知品种") && StringUtils.isEmpty(inCarReport.get(i).goodsTypeInfo))) {
                    i1 = j;
                    break;
                }
            }
            int i2 = -1;
            for (int j = 0; j < data.get(1).size(); j++) {
                if (data.get(1).get(j).title.equals(inCarReport.get(i).underCompany) || (data.get(1).get(j).title.equals("未知公司") && StringUtils.isEmpty(inCarReport.get(i).underCompany))) {
                    i2 = j;
                    break;
                }
            }
            int i3 = -1;
            for (int j = 0; j < data.get(2).size(); j++) {
                if (data.get(2).get(j).title.equals(inCarReport.get(i).contactName) || (data.get(2).get(j).title.equals("未知货主") && StringUtils.isEmpty(inCarReport.get(i).contactName))) {
                    i3 = j;
                    break;
                }
            }

            if (i1 >= 0) {
                if (data.get(0).get(i1).contentMap.containsKey(inCarReport.get(i).goodsInfo)) {
                    Integer integer = data.get(0).get(i1).contentMap.get(inCarReport.get(i).goodsInfo);
                    data.get(0).get(i1).contentMap.put(inCarReport.get(i).goodsInfo, integer + 1);
                } else {
                    data.get(0).get(i1).contentMap.put(inCarReport.get(i).goodsInfo, 1);
                }
            } else {
                ReportFormBean reportFormBean = new ReportFormBean();
                if (StringUtils.isEmpty(inCarReport.get(i).goodsTypeInfo)) {
                    reportFormBean.title = "未知品种";
                } else {
                    reportFormBean.title = inCarReport.get(i).goodsTypeInfo;
                }
                reportFormBean.contentMap.put(inCarReport.get(i).goodsInfo, 1);
                data.get(0).add(reportFormBean);
            }

            if (i2 >= 0) {
                if (data.get(1).get(i2).contentMap.containsKey(inCarReport.get(i).goodsInfo)) {
                    Integer integer = data.get(1).get(i2).contentMap.get(inCarReport.get(i).goodsInfo);
                    data.get(1).get(i2).contentMap.put(inCarReport.get(i).goodsInfo, integer + 1);
                } else {
                    data.get(1).get(i2).contentMap.put(inCarReport.get(i).goodsInfo, 1);
                }
            } else {
                ReportFormBean reportFormBean = new ReportFormBean();
                if (StringUtils.isEmpty(inCarReport.get(i).underCompany)) {
                    reportFormBean.title = "未知公司";
                } else {
                    reportFormBean.title = inCarReport.get(i).underCompany;
                }
                reportFormBean.contentMap.put(inCarReport.get(i).goodsInfo, 1);
                data.get(1).add(reportFormBean);
            }

            if (i3 >= 0) {
                if (data.get(2).get(i3).contentMap.containsKey(inCarReport.get(i).goodsInfo)) {
                    Integer integer = data.get(2).get(i3).contentMap.get(inCarReport.get(i).goodsInfo);
                    data.get(2).get(i3).contentMap.put(inCarReport.get(i).goodsInfo, integer + 1);
                } else {
                    data.get(2).get(i3).contentMap.put(inCarReport.get(i).goodsInfo, 1);
                }
            } else {
                ReportFormBean reportFormBean = new ReportFormBean();
                if (StringUtils.isEmpty(inCarReport.get(i).contactName)) {
                    reportFormBean.title = "未知货主";
                } else {
                    reportFormBean.title = inCarReport.get(i).contactName;
                }
                reportFormBean.contentMap.put(inCarReport.get(i).goodsInfo, 1);
                data.get(2).add(reportFormBean);
            }
        }
        for (int i = 0; i < data.get(0).size(); i++) {
            ReportFormBean reportFormBean = data.get(0).get(i);
            Set<String> strings = reportFormBean.contentMap.keySet();
            int count = 0;
            for (String s : strings) {
                reportFormBean.contents.add(s + "：" + reportFormBean.contentMap.get(s));
                count += reportFormBean.contentMap.get(s);
            }
            reportFormBean.title += ("：" + count);
        }
        for (int i = 0; i < data.get(1).size(); i++) {
            ReportFormBean reportFormBean = data.get(1).get(i);
            Set<String> strings = reportFormBean.contentMap.keySet();
            int count = 0;
            for (String s : strings) {
                reportFormBean.contents.add(s + "：" + reportFormBean.contentMap.get(s));
                count += reportFormBean.contentMap.get(s);
            }
            reportFormBean.title += ("：" + count);
        }
        for (int i = 0; i < data.get(2).size(); i++) {
            ReportFormBean reportFormBean = data.get(2).get(i);
            Set<String> strings = reportFormBean.contentMap.keySet();
            int count = 0;
            for (String s : strings) {
                reportFormBean.contents.add(s + "：" + reportFormBean.contentMap.get(s));
                count += reportFormBean.contentMap.get(s);
            }
            reportFormBean.title += ("：" + count);
        }
        return data;
    }
}
